
Imports std
Imports System.IO

Module combinew

    Public Const maxInputCount As Int32 = 1024
    Public Const selectAll As Char = character.asterisk

    Public Sub prepareIndexColumn(ByRef input As String, ByRef indexColumn As vector(Of String))
        Dim i As Int32
        Dim parts As vector(Of String) = Nothing
        If indexColumn Is Nothing Then
            indexColumn = New vector(Of String)
        Else
            indexColumn.clear()
        End If
        strsepar(input, character.comma + character.semicolon + character.sheffer + character.tab + character.blank _
                        + character.fullStop + character.leftSlash + character.rightSlash, parts)
        For i = 0 To parts.size() - 1
            indexColumn.push_back(parts(i))
        Next
    End Sub

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim reader(maxInputCount) As TextReader
        Dim writer As TextWriter = Nothing
        Dim inputCount As Int32
        Dim input(maxInputCount) As String
        Dim parts As vector(Of String) = Nothing
        Dim column(maxInputCount) As vector(Of String)
        Dim i As Int32
        Dim j As Int32
        Dim k As Int32
        Dim firstOfLine As Boolean = False

        If args.Length < 3 Or args.Length Mod 2 = 0 Then
            raiseError("argument count error, input1 column1,column2,... input2 column1,column2... " + _
                        "inputN column1,column2 ... output, if input or output is '/-', use console", _
                        errorHandle.errorType.critical)
        Else
            If strsame(args(args.Length - 1), "/-") Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(args.Length - 1), _
                                                FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not writer Is Nothing, "file no found, " + args(args.Length - 1))

            inputCount = 0
            For i = 0 To args.Length - 2 Step 2
                If strsame(args(i), "/-") Then
                    reader(inputCount) = Console.In
                Else
                    reader(inputCount) = New StreamReader(New FileStream(args(i), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                End If
                assert(Not reader(inputCount) Is Nothing, "file no found, " + args(i))
                prepareIndexColumn(args(i + 1), column(inputCount))
                inputCount += 1
            Next

            For j = 0 To inputCount - 1
                input(j) = Nothing
                input(j) = reader(j).ReadLine
            Next
            While Not containNothing(input, 0, inputCount)
                firstOfLine = True
                For j = 0 To inputCount - 1
                    kickNewlineChar(input(j))
                    strsepar(input(j), character.tab, parts)
                    For i = 0 To column(j).size() - 1
                        If column(j)(i) = selectAll Then
                            For k = 0 To parts.size() - 1
                                If Not firstOfLine Then
                                    writer.Write(character.tab)
                                Else
                                    firstOfLine = False
                                End If
                                writer.Write(parts(k))
                            Next
                        ElseIf Convert.ToUInt32(column(j)(i)) < parts.size() Then
                            If Not firstOfLine Then
                                writer.Write(character.tab)
                            Else
                                firstOfLine = False
                            End If
                            writer.Write(parts(Convert.ToUInt32(column(j)(i))))
                        End If
                    Next
                Next
                writer.WriteLine()

                For j = 0 To inputCount - 1
                    input(j) = Nothing
                    input(j) = reader(j).ReadLine
                Next
            End While

            For i = 0 To inputCount - 1
                closestream(reader(i))
            Next
            closestream(writer)
        End If
    End Sub

End Module
